package com.awphone.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author a_polyakov
 */
public class PasswordUtils {

    public static String sha2(final String password) {
        String result = null;
        try {
            MessageDigest sha2 = MessageDigest.getInstance("SHA-512");
            byte messageDigest[] = sha2.digest(password.getBytes("UTF-8"));
            result = base64(messageDigest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(final String password) {
        String result = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            byte messageDigest[] = md5.digest(password.getBytes("UTF-8"));
            result = base64(messageDigest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String base64(byte messageDigest[]) {
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < messageDigest.length; i++) {
            String currentByte = Integer.toHexString(0xFF & messageDigest[i]);
            if (currentByte.length() == 1) {
                currentByte = "0" + currentByte;
            }
            hexString.append(currentByte);
        }
        return hexString.toString();
    }
}
